﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Security.Principal;
using System.Web.Mvc;

namespace Kratos.Framework.ContentManagement.Models.SystemRights
{
    public class UserInfo : IPrincipal
    {
        public UserInfo()
        {
            _actionPermissions = new List<ActionPermission>();
            _groups = new List<Group>();
        }

        [HiddenInput(DisplayValue = false)]
        [ScaffoldColumn(false)]
        public virtual int Id { get; set; }

        [Required(ErrorMessage = "用户名不能为空")]
        public virtual string UserId { get; set; }

        [Required(ErrorMessage = "密码不能为空")]
        [Compare("Password2", ErrorMessage = "两次输入的密码不一致")]
        [DataType(DataType.Password)]
        public virtual string Password { get; set; }

        [Compare("Password", ErrorMessage = "两次输入的密码不一致")]
        [Required(ErrorMessage = "确认密码不能为空")]
        [DataType(DataType.Password)]
        public virtual string Password2 { get; set; }

        [Required(ErrorMessage = "名不能为空")]
        public virtual string FirstName { get; set; }

        [Required(ErrorMessage = "姓不能为空")]
        public virtual string LastName { get; set; }

        [ScaffoldColumn(false)]
        public virtual string FullName { get { return FirstName + " " + LastName; } }

        public virtual DateTime? CreateTime { get; set; }

        public virtual DateTime? LastLogOnTime { get; set; }

        private IList<ActionPermission> _actionPermissions;
        /// <summary>
        /// 权限列表
        /// </summary>
        public virtual IList<ActionPermission> ActionPermissions
        {
            get { return _actionPermissions; }
            set { _actionPermissions = value; }
        }

        private IList<Group> _groups;
        /// <summary>
        /// 所属的用户组列表
        /// </summary>
        public virtual IList<Group> Groups
        {
            get { return _groups; }
            set { _groups = value; }
        }

        #region IPrincipal成员
        public virtual IIdentity Identity
        {
            get
            {
                bool isAuthenticated = Id > 0 && UserId.Trim() != "";
                return new Identity(isAuthenticated, UserId);
            }
        }

        [Obsolete("暂时不需要角色", true)]
        public virtual bool IsInRole(string role)
        {
            return true;
        }
        #endregion

        #region 公有方法
        public virtual void AddActionPermission(ActionPermission action)
        {
            _actionPermissions.Add(action);
        }

        #endregion
    }

    /// <summary>
    /// 用户标识
    /// </summary>
    public class Identity : IIdentity
    {
        private readonly bool _isAuthenticated;
        private readonly string _name;

        public Identity(bool isAuthenticated, string name)
        {
            _isAuthenticated = isAuthenticated;
            _name = name;
        }

        public string AuthenticationType
        {
            get { return "Forms"; }
        }

        public bool IsAuthenticated
        {
            get { return _isAuthenticated; }
        }

        public string Name
        {
            get { return _name; }
        }
    }
}
